Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: group declarations when printing module #1133

Merged
merged 4 commits into from
Mar 27, 2024
Merged

Conversation

matt2e
Copy link
Collaborator

@matt2e matt2e commented Mar 25, 2024

#1089

Ordering and spacing is the following (example has 2 of each declaration type)

{
   config
   config
   secret
   secret

   database
   database

   enum

   enum

   data

   data

   verb

   verb
}

Full example:

module echo {
  config default String
  config default2 String
  secret default3 String
  secret default3 String

  // An echo request.
  data EchoRequest {
    name String? +alias json "name"
    inner echo.EchoRequest2 +alias json "inner"
  }

  data EchoRequest2 {
    name String? +alias json "name"
  }

  data EchoResponse {
    message String +alias json "message"
    inner echo.EchoResponse2 +alias json "inner"
  }

  data EchoResponse2 {
    message String +alias json "message"
  }

  // Echo returns a greeting with the current time.
  verb echo(echo.EchoRequest) echo.EchoResponse  
      +calls time.time

  verb echo2(echo.EchoRequest) echo.EchoResponse  
      +calls time.time
}

@matt2e matt2e linked an issue Mar 25, 2024 that may be closed by this pull request

// print decls in groups
groupedTypes := []struct {
types []interface{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/interface{}/any/g

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alecthomas do you know if this is "lintable" It might be a good check for the other TBD team as well. :)


hasPrintedDecl = true
hasPrintedDeclInGroup = true
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declarations should already be sorted in the correct order, so you could simplify this considerably by just iterating over them and printing the correct spacing as you transition between groups/lines.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to trust the exisiting order, and use reflect.Type rather than empty structs which makes it easier to access rules from the mapping

@matt2e matt2e requested a review from alecthomas March 26, 2024 04:11
@alecthomas
Copy link
Collaborator

Nice!

@matt2e matt2e merged commit 11b2561 into main Mar 27, 2024
11 checks passed
@matt2e matt2e deleted the matt2e/tidy-schema-output branch March 27, 2024 05:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Order and group FTL schema output in a nicer way
3 participants